package jse.test20160807.reflectGetFiled;

import org.junit.Test;

import java.lang.reflect.Field;

/**
 * @desc通过反射改变私有属性
 *
 * @author ${user}
 * @create 2016 08 07 21:31
 */
public class TestGetFiled {

    @Test
    public void testGet() {

        Student student = new Student(100,"价值");

        Class<?> clazz = student.getClass();
        System.out.print("类的标准名称："+clazz.getCanonicalName());

        try {
            Field id = clazz.getDeclaredField("id");
            System.out.println("设置前的id："+student.getId());
            id.setAccessible(true);
            id.setInt(student,10);
            System.out.println("射击后的id:"+student.getId());

            Field name = clazz.getDeclaredField("name");
            System.out.println("射击前的name:"+student.getName());
            name.setAccessible(true);
            name.set(student, "扯淡");
            System.out.println("射击后的name:"+student.getName()) ;

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
